﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Studio7i.CapaNegocio;
using Studio7i.CapaDominio;

namespace Studio7i.Controllers
{
    public class SalaController : Controller
    {
        //
        // GET: /Sala/

        #region Dependencias a inyectarse con Spring.NET
        IAdministradorService AdminService = null;
        #endregion


        public ActionResult Index()
        {
            ICollection<Sala> modelo = AdminService.ListarSalas(
               null, null, null, null);
            TempData["locales"] = ListadoLocales();
            return View(modelo);
        }

        //
        // GET: /Sala/Details/5

        public ActionResult Details(int codigo)
        {
            Sala modelo = AdminService.ObtenerSala(codigo);
            return View(modelo);
        }

        //
        // GET: /Sala/Create

        public ActionResult Inscripcion()
        {
            return View();
        } 

        //
        // POST: /Sala/Create

        [HttpPost]
        public ActionResult Inscribir(FormCollection formulario)
        {
            try
            {
                string Nombre = formulario["NoSala"];
                if (formulario["LocalCodigo"] == null || formulario["LocalCodigo"] == string.Empty)
                    throw new Exception("Elija un local");
                int LocalCodigo = int.Parse(formulario["LocalCodigo"]);
                int Capacidad = int.Parse(formulario["QtCapacidad"]);
                string Caracteristicas = formulario["TxCaracteristicas"];                
                Sala salaInscrita = AdminService.InscribirSala(Nombre, LocalCodigo, Capacidad, Caracteristicas);
                TempData["mensaje"] = "Sala " + salaInscrita.CoSala + " inscrita satisfactoriamente";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TempData["locales"] = ListadoLocales();
                return View("Inscripcion");
            }
        }

        public ActionResult BusquedaSalasArmadas()
        {
            return View();
        } 

        //
        [HttpPost]
        public ActionResult BuscarSalasArmadas(FormCollection formulario)
        {
            string Nombre = formulario["Nombre"];
            int? LocalCodigo = null;
            if (formulario["LocalCodigo"] != null && formulario["LocalCodigo"] != string.Empty)
                LocalCodigo = int.Parse(formulario["LocalCodigo"]);
            int? Capacidad = null;
            if (formulario["Capacidad"] != null && formulario["Capacidad"] != string.Empty)
                Capacidad = int.Parse(formulario["Capacidad"]);
            string Caracteristicas = formulario["Caracteristicas"];
            ICollection<Sala> modelo = AdminService.ListarSalas(
                Nombre, LocalCodigo, Capacidad, Caracteristicas);
            TempData["locales"] = ListadoLocales();
            return View("BusquedaSalasArmadas", modelo);
        }
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Sala/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Sala/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Sala/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        private ICollection<SelectListItem> ListadoLocales()
        {
            ICollection<SelectListItem> locales = new List<SelectListItem>();
            locales.Add(new SelectListItem() { Value = string.Empty, Text = "Seleccione.." });
            foreach (Local local in AdminService.ListarLocales())
                locales.Add(new SelectListItem()
                {
                    Value = local.CoLocal.ToString(),
                    Text = local.NoLocal
                });
            return locales;
        }
    }
}
